package com.sinbader.xiaole.common.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.UserKit;
import com.alibaba.fastjson.JSON;
import com.sinbader.xiaole.common.entity.Stock;
import com.sinbader.xiaole.common.entity.StockLog;
import com.sinbader.xiaole.common.enums.StockOperationTypes;
import com.sinbader.xiaole.common.kit.HttpKit;
import com.sinbader.xiaole.common.mapper.StockMapper;
import com.sinbader.xiaole.common.model.params.StockParam;
import com.sinbader.xiaole.common.model.result.StockResult;
import  com.sinbader.xiaole.common.service.StockService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 库存 服务实现类
 */
@Service
public class StockServiceImpl extends ServiceImpl<StockMapper, Stock> implements StockService {

    @Override
    public void add(StockParam param){
        Stock entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(StockParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(StockParam param){
        Stock oldEntity = getOldEntity(param);
        Stock newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public StockResult findBySpec(StockParam param){
        return null;
    }

    @Override
    public List<StockResult> findListBySpec(StockParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(StockParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(StockParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Stock getOldEntity(StockParam param) {
        return this.getById(getKey(param));
    }

    private Stock getEntity(StockParam param) {
        Stock entity = new Stock();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    public Stock getByShopIdAndProductId(Long shopId, Long productId) {
    		return getBaseMapper().getByShopIdAndProductId(shopId, productId);
	}


    public void initEmpty(Stock stock) {
		stock.setSaleUsableCount(0);
		stock.setSaleFreezeCount(0);
		stock.setSaleUsedCount(0);
		stock.setWarehouseUsableCount(0);
		stock.setWarehouseFreezeCount(0);
		stock.setWarehouseUsedCount(0);
		stock.setCreateTime(new Date());
		stock.setCreatorId(UserKit.getUserId());
	}

	@Transactional
    public void change(StockLog stockLog) {
		Stock stock = getById(stockLog.getStockId());

	}

	@Transactional
	public void change(String operationType, String orderType, Long orderId) {

	}

	@Transactional(propagation = Propagation.REQUIRED)
	public void change(Stock stock, String operationType, int count) {
    	Stock oldStock = JSON.parseObject(JSON.toJSONString(stock), Stock.class);
		if(StockOperationTypes.Inner.equals(operationType)) { // 入库
			stock.changeSaleUsableCount(+count);
			stock.changeWarehouseUsableCount(+count);
		} else if(StockOperationTypes.OuterFromUsable.equals(operationType)) { // 出库 （从可用库存）
			stock.changeSaleUsableCount(-count);
			stock.changeSaleUsedCount(+count);
			stock.changeWarehouseUsableCount(-count);
			stock.changeWarehouseUsedCount(+count);
		} else if(StockOperationTypes.OuterFromFreeze.equals(operationType)) { // 出库（从冻结库存）
			stock.changeSaleFreezeCount(-count);
			stock.changeSaleUsedCount(+count);
			stock.changeWarehouseFreezeCount(-count);
			stock.changeWarehouseUsedCount(+count);
		} else if(StockOperationTypes.SaleOrderPrepare.equals(operationType)) { // 销售订单 - 预下单
			stock.changeSaleUsableCount(-count);
			stock.changeSaleFreezeCount(+count);
			stock.changeWarehouseUsableCount(-count);
			stock.changeWarehouseFreezeCount(+count);
		} else if(StockOperationTypes.SaleOrderCancel.equals(operationType)) { // 销售订单 - 取消
			stock.changeSaleUsableCount(+count);
			stock.changeSaleFreezeCount(-count);
			stock.changeWarehouseUsableCount(+count);
			stock.changeWarehouseFreezeCount(-count);
		} else if(StockOperationTypes.SaleOrderPay.equals(operationType)) { // 销售订单 - 支付
			stock.changeSaleFreezeCount(-count);
		}

		stock.nextVersion();
		stock.checkCounts();
		updateById(stock);

		
	}

	public static void main(String[] args) {
		int i = -3;
		int i2 = -i;
		System.out.println(i);
		System.out.println(i2);
	}

}
